package com.tarena.lbs.coupon.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 读取字符串值
    public String getValue(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }
    public void setValueWithExpire(String key, String value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }
    // 读取对象
    public Object getObject(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 判断key是否存在
    public boolean exists(String key) {
        return Boolean.TRUE.equals(redisTemplate.hasKey(key));
    }

    // 批量读取
    public List<Object> multiGet(List<String> keys) {
        return redisTemplate.opsForValue().multiGet(keys);
    }

    // 获取匹配的keys
    public Set<String> getKeys(String pattern) {
        return redisTemplate.keys(pattern);
    }
}